package com.atguigu.yygh.common.exception;

import com.atguigu.yygh.common.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @program: yygh_parent
 * @author: Mr.Zhuo
 * @create: 2022-03-07 20:39
 **/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public R exception(Exception e) {
        return R.error().message("全局处理异常");
    }

    @ResponseBody
    @ExceptionHandler(ArithmeticException.class)
    public R arithmeticException(ArithmeticException e) {
        return R.error().message("算数异常~");
    }

    @ResponseBody
    @ExceptionHandler(YyghException.class)
    public R yyghException(YyghException e) {
        log.error(e.getMsg());
        return R.error().message(e.getMsg()).code(e.getCode());
    }
}
